package com.sdjzu.wanfuying.androiddesign.mvvm.student;

import android.os.Bundle;
import android.view.ViewGroup;

import com.sdjzu.wanfuying.androiddesign.R;
import com.sdjzu.wanfuying.androiddesign.base.BaseActivity;
import com.sdjzu.wanfuying.androiddesign.databinding.StudentListItemViewDataBinding;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class StudentRecyclerListActivity extends BaseActivity {
    @BindView(R.id.activity_student_recycler_view_recyclerview)
    RecyclerView mRecyclerView;
    private StudentRecyclerListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_recycler_list);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(adapter = new StudentRecyclerListAdapter());
        loopOnUiThread(() -> adapter.refresh(), 1000, 1000);
    }

    class StudentRecyclerListAdapter extends RecyclerView.Adapter<StudentRecyclerListAdapter.ViewHolder> {
        private final static int STUDENT_NUM = 10;
        private ArrayList<Student> studentList = new ArrayList<>(STUDENT_NUM);

        public StudentRecyclerListAdapter() {
            studentList.clear();
            for (int i = 0; i < STUDENT_NUM; i++) {
                Student student = new Student("2014000" + i, Math.random() + "");
                student.setAvatarUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1548663767481&di=273eafa024cfdbcb29764b0dfaa66fde&imgtype=0&src=http%3A%2F%2Fpic23.nipic.com%2F20120904%2F6772262_115609168000_2.jpg");
                studentList.add(student);
            }
        }

        public void refresh() {
            for (Student student : studentList) {
                student.setName(Math.random() + "");
            }
        }

        @NonNull
        @Override
        public StudentRecyclerListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ViewDataBinding viewDataBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_student_list_item, parent, false);
            return new ViewHolder(viewDataBinding);
        }

        @Override
        public void onBindViewHolder(@NonNull StudentRecyclerListAdapter.ViewHolder holder, int position) {
            ((StudentListItemViewDataBinding) holder.getViewDataBinding()).setStudent(studentList.get(position));
        }

        @Override
        public int getItemCount() {
            return studentList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            private ViewDataBinding viewDataBinding;

            public ViewHolder(ViewDataBinding viewDataBinding) {
                super(viewDataBinding.getRoot().getRootView());
                this.viewDataBinding = viewDataBinding;
            }

            public ViewDataBinding getViewDataBinding() {
                return viewDataBinding;
            }
        }
    }
}
